/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogoServidor.java
 *
 * Created on 03/08/2009, 01:06:22
 */

package br.org.espmessenger.cliente.telas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import br.org.espmessenger.cliente.Fachada;
import br.org.espmessenger.cliente.Servidor;
import br.org.espmessenger.middleware.InfraEstruturaException;

public class DialogoServidor extends javax.swing.JDialog {
	private DialogoServidorListener listener;
	private Servidor servidor;
	private Fachada fachada;
	
    /** Creates new form DialogoServidor */
    public DialogoServidor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        listener = new DialogoServidorListener();
        try {
			fachada = Fachada.getInstance();
		} catch (InfraEstruturaException e) {
			JOptionPane.showMessageDialog(DialogoServidor.this,
					e.getMessage(), "Falha!", JOptionPane.ERROR_MESSAGE);
		}
        getServidor();
        initComponents();
    }
    
    public Servidor getServidor() {
    	servidor = new Servidor();
		servidor.setHost(fachada.getEntrada("servidor.host").toString());
		servidor.setPorta((Integer.parseInt(fachada.getEntrada("servidor.porta").toString())));
		servidor.setServico(fachada.getEntrada("servidor.servico").toString());

    	return servidor;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        txtHost = new javax.swing.JTextField();
        spinPorta = new javax.swing.JSpinner();
        txtServico = new javax.swing.JTextField();
        btnDefinir = new javax.swing.JButton();
        btnTestar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Definir Servidor");

        jLabel1.setText("Host:");

        jLabel2.setText("Porta:");

        jLabel3.setText("Serviço:");

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 0, 16)); // NOI18N
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/org/espmessenger/cliente/icones/grandes/servidor.png"))); // NOI18N

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        txtHost.setText(servidor.getHost());
        
        spinPorta.setModel(new javax.swing.SpinnerNumberModel(1099, 1025, 65535, 1));
        spinPorta.setValue(servidor.getPorta());
        
        txtServico.setText(servidor.getServico());

        btnDefinir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/org/espmessenger/cliente/icones/pequenos/servidor.png"))); // NOI18N
        btnDefinir.setMnemonic('D');
        btnDefinir.setText("Definir servidor");
        btnDefinir.addActionListener(listener);
        btnDefinir.setActionCommand("definir");

        btnTestar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/org/espmessenger/cliente/icones/pequenos/conectar.png"))); // NOI18N
        btnTestar.setMnemonic('T');
        btnTestar.setText("Testar conexão");
        btnTestar.addActionListener(listener);
        btnTestar.setActionCommand("testar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtHost, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                            .addComponent(spinPorta, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtServico, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnTestar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDefinir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(spinPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(txtServico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnDefinir)
                            .addComponent(btnTestar))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogoServidor dialog = new DialogoServidor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDefinir;
    private javax.swing.JButton btnTestar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSpinner spinPorta;
    private javax.swing.JTextField txtHost;
    private javax.swing.JTextField txtServico;
    // End of variables declaration//GEN-END:variables

    
    private class DialogoServidorListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			Servidor servidor = new Servidor(txtHost.getText(),
					(Integer) spinPorta.getValue(),
					txtServico.getText());
			if ("testar".equals(e.getActionCommand())) {
				try {
					Fachada.getInstance().testarServidor(servidor);
					JOptionPane.showMessageDialog(DialogoServidor.this,
							"Teste realizado com sucesso!",
							"Servidor ativo!",
							JOptionPane.INFORMATION_MESSAGE);
				} catch (InfraEstruturaException e1) {
					JOptionPane.showMessageDialog(DialogoServidor.this,
							e1.getMessage(),
							"Falha!",
							JOptionPane.ERROR_MESSAGE);
				}
				
			} else if ("definir".equals(e.getActionCommand())) {
				fachada.definirServidor(servidor);
				// Fecha (setVisible(false) e apaga as referências internas à janela aberta)
				DialogoServidor.this.dispose();
			}
		}
		
	}


}
